﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chat.Models.Services;
using Chat.Models;

namespace Chat.Models.Services
{
    public class UserService
    {
        static public string GetLoginToken(int uid)
        {
                SessionMoudel sessionMoudel = new SessionMoudel();
                sessionMoudel.UserID = uid;
                sessionMoudel.webSocketConnectionID = null;
                string token = Guid.NewGuid().ToString();
                global::TokenStore.SetCachedData(token, sessionMoudel);
                return token;
        }

        static public Users GetUserByPhoneNumberAndPassword(string phoneNumber, string passwd)
        {
            var db = new chatEntities();
            var res = from item in db.Users where phoneNumber == item.PhoneNumber && passwd == item.Password select item;
            if (res.Count() > 0)
            {
                return res.First();
            }

            return null;
        }
        static public Users GetUsersByUserId(int UserId)
        {
            var db = new chatEntities();
            var res = from item in db.Users where UserId == item.UserID select item;
            if (res.Count() > 0)
            {
                return res.First();
            }

            return null;
        }

        static public Users GetUsersByPhoneNumber(string phoneNumber)
        {
            var db = new chatEntities();
            var res = from item in db.Users where phoneNumber == item.PhoneNumber select item;
            if (res.Count() > 0)
            {
                return res.First();
            }
            return null;
        }

        static public bool addUser(RegistMoudel registMoudel)
        {
            Users user = new Users();

            user.UserName = registMoudel.UserName;
            user.Password = registMoudel.Password;
            user.PhoneNumber = registMoudel.PhoneNumber;
            user.AvatarImageURL = "/image/default.jpg";
            user.Age = 0;
            user.Gender = true;
            user.LikesCount = 0;
    

            var db = new chatEntities();

            db.Users.Add(user);

            // 提交更改到数据库
            return db.SaveChanges()!=0;

        }

        public static void GiveALike(int uid)
        {
            var db = new chatEntities();
            var res = from item in db.Users where item.UserID == uid select item;

            if (res.Count()>0)
            {
                res.First().LikesCount++;
                db.SaveChanges();
            }
        }

        public static bool AddFriend(int uid1, int uid2)
        {
            try
            {
                var UserA = GetUsersByUserId(uid1);
                var UserB = GetUsersByUserId(uid2);

                if (UserA != null && UserB != null)
                {
                    var db = new chatEntities();
                    Friendships friendshipA = new Friendships();
                    Friendships friendshipB = new Friendships();


                    friendshipA.UserID1 = uid1;
                    friendshipA.UserID2 = uid2;

                    friendshipB.UserID1 = uid2;
                    friendshipB.UserID2 = uid1;



                    db.Friendships.Add(friendshipA);

                    db.Friendships.Add(friendshipB);

                    return db.SaveChanges() == 2;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        static public List<Users> getFriendsByUserId(int UserId)
        {
            var list=new List<Users>();

            var db = new chatEntities();
            var res = from item in db.Friendships where item.UserID1==UserId select item;
            foreach(var it in res)
            {
                var user=from item in db.Users where item.UserID==it.UserID2 select item;
                list.Add(user.First());
            }
            return list;

        }


        static public Friendships GetFriendshipsByUserIDAAndUserB(int userIdA, int userIdB)
        {
            try
            {
                var db = new chatEntities();
                var res = from item in db.Friendships where item.UserID1 == userIdA && item.UserID2 == userIdB select item;
                if (res.Count() > 0)
                {
                    return res.First();
                }

                return null;
            }
            catch
            {
                return null;
            }

        }
        static public Users GetUserByPhoneNumber(string PhoneNumber)
        {
            try
            {
                var db = new chatEntities();
                var res = from item in db.Users where item.PhoneNumber == PhoneNumber select item;
                if (res.Count() > 0)
                {
                    return res.First();
                }

                return null;
            }
            catch
            {
                return null;
            }

        }

        static public IndexModel getIndexData(int UserId)
        {
            var db = new chatEntities();
            IndexModel indexModel = new IndexModel();

            indexModel.User=GetUsersByUserId(UserId);
            indexModel.FriendsAndLastMessage = new List<IndexModel.FriendAndLastMessage>();
            List<Users> users = getFriendsByUserId(UserId);

            foreach(var user in users){
                ChatMessages chat =ChatMessageService.GetLastChatMessage(UserId, user.UserID);
                var friendAndLastMessage = new IndexModel.FriendAndLastMessage();
                friendAndLastMessage.friend = user;
                friendAndLastMessage.LastMessage = chat;
                indexModel.FriendsAndLastMessage.Add(friendAndLastMessage);
            }

            return indexModel;
        }

        public static  bool updateUserById(int uid,Users newUser)
        {
            try
            {
                var db = new chatEntities();
                var res=from item in db.Users where item.UserID==uid select item;

                if(res.Count() > 0)
                {
                    var user=res.First();
                    user.Age=newUser.Age;
                    user.UserName=newUser.UserName;
                    user.AvatarImageURL = newUser.AvatarImageURL;
                    user.PersonalSignature=newUser.PersonalSignature;
                    if(newUser.Password!=null&&newUser.Password!="")
                        user.Password = newUser.Password;
                    user.Gender = newUser.Gender;

                    return db.SaveChanges()!=0;
                }
                else
                {
                    return false;
                }
            }catch{
                return false;
            }
        }
    }
}